<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="主键" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入主键" />
                </el-form-item>
                <el-form-item label="用户编码" prop="sn">
                    <el-input v-model="formData.sn" placeholder="请输入用户编码" />
                </el-form-item>
                <el-form-item label="头像" prop="avatar">
                    <el-input v-model="formData.avatar" placeholder="请输入头像" />
                </el-form-item>
                <el-form-item label="真实姓名" prop="real_name">
                    <el-input v-model="formData.real_name" placeholder="请输入真实姓名" />
                </el-form-item>
                <el-form-item label="用户昵称" prop="nickname">
                    <el-input v-model="formData.nickname" placeholder="请输入用户昵称" />
                </el-form-item>
                <el-form-item label="用户账号" prop="username">
                    <el-input v-model="formData.username" placeholder="请输入用户账号" />
                </el-form-item>
                <el-form-item label="用户电话" prop="mobile">
                    <el-input v-model="formData.mobile" placeholder="请输入用户电话" />
                </el-form-item>
                <el-form-item label="手机订阅PUSH推送标识" prop="regid">
                    <el-input v-model="formData.regid" placeholder="请输入手机订阅PUSH推送标识" />
                </el-form-item>
                <el-form-item label="设备唯一标识" prop="device_id">
                    <el-input v-model="formData.device_id" placeholder="请输入设备唯一标识" />
                </el-form-item>
                <el-form-item label="手机设备厂商" prop="provider">
                    <el-input v-model="formData.provider" placeholder="请输入手机设备厂商" />
                </el-form-item>
                <el-form-item label="设备型号" prop="model">
                    <el-input v-model="formData.model" placeholder="请输入设备型号" />
                </el-form-item>
                <el-form-item label="用户性别: [1=男, 2=女, 0=未知]" prop="sex">
                    <el-select class="flex-1" v-model="formData.sex" placeholder="请选择用户性别: [1=男, 2=女, 0=未知]">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="币-购买" prop="coin_buy">
                    <el-input v-model="formData.coin_buy" placeholder="请输入币-购买" />
                </el-form-item>
                <el-form-item label="币-赠送" prop="coin_gift">
                    <el-input v-model="formData.coin_gift" placeholder="请输入币-赠送" />
                </el-form-item>
                <el-form-item label="用户类型: [1=普通用户, 2=vip用户]" prop="user_type">
                    <el-select class="flex-1" v-model="formData.user_type" placeholder="请选择用户类型: [1=普通用户, 2=vip用户]">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="会员过期时间" prop="vip_time">
                    <el-input v-model="formData.vip_time" placeholder="请输入会员过期时间" />
                </el-form-item>
                <el-form-item label="是否添加桌面: [0=否, 1=是]" prop="add_desktop">
                    <el-input v-model="formData.add_desktop" placeholder="请输入是否添加桌面: [0=否, 1=是]" />
                </el-form-item>
                <el-form-item label="渠道编码" prop="source_id">
                    <el-input v-model="formData.source_id" placeholder="请输入渠道编码" />
                </el-form-item>
                <el-form-item label="[1=短剧, 2=工具 3=小说]" prop="account_id">
                    <el-input v-model="formData.account_id" placeholder="请输入[1=短剧, 2=工具 3=小说]" />
                </el-form-item>
                <el-form-item label="渠道编码-激活" prop="source_id_active">
                    <el-input v-model="formData.source_id_active" placeholder="请输入渠道编码-激活" />
                </el-form-item>
                <el-form-item label="激活时间" prop="active_time">
                    <el-input v-model="formData.active_time" placeholder="请输入激活时间" />
                </el-form-item>
                <el-form-item label="注册渠道: [1=微信小程序, 2=微信公众号, 3=手机H5, 4=电脑PC, 5=苹果APP, 6=安卓APP, 7=快应用, 8=抖音小程序]" prop="channel">
                    <el-input v-model="formData.channel" placeholder="请输入注册渠道: [1=微信小程序, 2=微信公众号, 3=手机H5, 4=电脑PC, 5=苹果APP, 6=安卓APP, 7=快应用, 8=抖音小程序]" />
                </el-form-item>
                <el-form-item label="应用包名" prop="package_name">
                    <el-input v-model="formData.package_name" placeholder="请输入应用包名" />
                </el-form-item>
                <el-form-item label="应用版本号" prop="version">
                    <el-input v-model="formData.version" placeholder="请输入应用版本号" />
                </el-form-item>
                <el-form-item label="应用最后登录版本号" prop="version_last">
                    <el-input v-model="formData.version_last" placeholder="请输入应用最后登录版本号" />
                </el-form-item>
                <el-form-item label="注册IP" prop="register_ip">
                    <el-input v-model="formData.register_ip" placeholder="请输入注册IP" />
                </el-form-item>
                <el-form-item label="最后登录IP" prop="last_login_ip">
                    <el-input v-model="formData.last_login_ip" placeholder="请输入最后登录IP" />
                </el-form-item>
                <el-form-item label="请求用户UA" prop="register_ua">
                    <el-input
                        v-model="formData.register_ua"
                        placeholder="请输入请求用户UA"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="上次登录UA" prop="last_ua">
                    <el-input
                        v-model="formData.last_ua"
                        placeholder="请输入上次登录UA"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="城市信息" prop="city_info">
                    <el-input v-model="formData.city_info" placeholder="请输入城市信息" />
                </el-form-item>
                <el-form-item label="回传状态 0未回传 1已回传" prop="callback_status">
                    <el-radio-group v-model="formData.callback_status" placeholder="请选择回传状态 0未回传 1已回传">
                        <el-radio label="0">请选择字典生成</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否异常: [0=正常, 1=异常]" prop="is_disable">
                    <el-radio-group v-model="formData.is_disable" placeholder="请选择是否异常: [0=正常, 1=异常]">
                        <el-radio label="0">请选择字典生成</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="[1=短剧, 2=工具 3=小说]" prop="is_jump">
                    <el-input v-model="formData.is_jump" placeholder="请输入[1=短剧, 2=工具 3=小说]" />
                </el-form-item>
                <el-form-item label="最后登录时间" prop="last_login_time">
                    <el-input v-model="formData.last_login_time" placeholder="请输入最后登录时间" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  userDetail } from '@/api/client/client_user'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑用户信息' : '新增用户信息'
})

const formData = reactive({
    id: '',
    sn: '',
    avatar: '',
    real_name: '',
    nickname: '',
    username: '',
    mobile: '',
    regid: '',
    device_id: '',
    provider: '',
    model: '',
    sex: '',
    coin_buy: '',
    coin_gift: '',
    user_type: '',
    vip_time: '',
    add_desktop: '',
    source_id: '',
    account_id: '',
    source_id_active: '',
    active_time: '',
    channel: '',
    package_name: '',
    version: '',
    version_last: '',
    register_ip: '',
    last_login_ip: '',
    register_ua: '',
    last_ua: '',
    city_info: '',
    callback_status: '',
    is_disable: '',
    is_jump: '',
    last_login_time: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键',
            trigger: ['blur']
        }
    ],
    sn: [
        {
            required: true,
            message: '请输入用户编码',
            trigger: ['blur']
        }
    ],
    avatar: [
        {
            required: true,
            message: '请输入头像',
            trigger: ['blur']
        }
    ],
    real_name: [
        {
            required: true,
            message: '请输入真实姓名',
            trigger: ['blur']
        }
    ],
    nickname: [
        {
            required: true,
            message: '请输入用户昵称',
            trigger: ['blur']
        }
    ],
    username: [
        {
            required: true,
            message: '请输入用户账号',
            trigger: ['blur']
        }
    ],
    mobile: [
        {
            required: true,
            message: '请输入用户电话',
            trigger: ['blur']
        }
    ],
    regid: [
        {
            required: true,
            message: '请输入手机订阅PUSH推送标识',
            trigger: ['blur']
        }
    ],
    device_id: [
        {
            required: true,
            message: '请输入设备唯一标识',
            trigger: ['blur']
        }
    ],
    provider: [
        {
            required: true,
            message: '请输入手机设备厂商',
            trigger: ['blur']
        }
    ],
    model: [
        {
            required: true,
            message: '请输入设备型号',
            trigger: ['blur']
        }
    ],
    sex: [
        {
            required: true,
            message: '请选择用户性别: [1=男, 2=女, 0=未知]',
            trigger: ['blur']
        }
    ],
    coin_buy: [
        {
            required: true,
            message: '请输入币-购买',
            trigger: ['blur']
        }
    ],
    coin_gift: [
        {
            required: true,
            message: '请输入币-赠送',
            trigger: ['blur']
        }
    ],
    user_type: [
        {
            required: true,
            message: '请选择用户类型: [1=普通用户, 2=vip用户]',
            trigger: ['blur']
        }
    ],
    vip_time: [
        {
            required: true,
            message: '请输入会员过期时间',
            trigger: ['blur']
        }
    ],
    add_desktop: [
        {
            required: true,
            message: '请输入是否添加桌面: [0=否, 1=是]',
            trigger: ['blur']
        }
    ],
    source_id: [
        {
            required: true,
            message: '请输入渠道编码',
            trigger: ['blur']
        }
    ],
    account_id: [
        {
            required: true,
            message: '请输入[1=短剧, 2=工具 3=小说]',
            trigger: ['blur']
        }
    ],
    source_id_active: [
        {
            required: true,
            message: '请输入渠道编码-激活',
            trigger: ['blur']
        }
    ],
    active_time: [
        {
            required: true,
            message: '请输入激活时间',
            trigger: ['blur']
        }
    ],
    channel: [
        {
            required: true,
            message: '请输入注册渠道: [1=微信小程序, 2=微信公众号, 3=手机H5, 4=电脑PC, 5=苹果APP, 6=安卓APP, 7=快应用, 8=抖音小程序]',
            trigger: ['blur']
        }
    ],
    package_name: [
        {
            required: true,
            message: '请输入应用包名',
            trigger: ['blur']
        }
    ],
    version: [
        {
            required: true,
            message: '请输入应用版本号',
            trigger: ['blur']
        }
    ],
    version_last: [
        {
            required: true,
            message: '请输入应用最后登录版本号',
            trigger: ['blur']
        }
    ],
    register_ip: [
        {
            required: true,
            message: '请输入注册IP',
            trigger: ['blur']
        }
    ],
    last_login_ip: [
        {
            required: true,
            message: '请输入最后登录IP',
            trigger: ['blur']
        }
    ],
    register_ua: [
        {
            required: true,
            message: '请输入请求用户UA',
            trigger: ['blur']
        }
    ],
    last_ua: [
        {
            required: true,
            message: '请输入上次登录UA',
            trigger: ['blur']
        }
    ],
    city_info: [
        {
            required: true,
            message: '请输入城市信息',
            trigger: ['blur']
        }
    ],
    callback_status: [
        {
            required: true,
            message: '请选择回传状态 0未回传 1已回传',
            trigger: ['blur']
        }
    ],
    is_disable: [
        {
            required: true,
            message: '请选择是否异常: [0=正常, 1=异常]',
            trigger: ['blur']
        }
    ],
    is_jump: [
        {
            required: true,
            message: '请输入[1=短剧, 2=工具 3=小说]',
            trigger: ['blur']
        }
    ],
    last_login_time: [
        {
            required: true,
            message: '请输入最后登录时间',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await userEdit(data) : await userAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await userDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
